package com.youxin.cpcenter.common.cors;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.CorsConfigurationSource;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;

import com.youxin.busfoundation.common.BuildConfig;

@Configuration
public class RestConfig {

    @Bean
    public CORSFilter corsFilter() {
        CorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        CorsConfiguration config = new CorsConfiguration();
        //config.addAllowedOrigin(BuildConfig.isProduction()?"http://qiye.slsai.com,https://qiye.slsai.com":"*");
//        if(BuildConfig.isProduction())
//        {
//        	config.addAllowedOrigin("http://qiye.slsai.com");
//        	config.addAllowedOrigin("https://qiye.slsai.com");
//        }
//        else
      //  {
        	config.addAllowedOrigin("*");
       // }
      //  config.setMaxAge(maxAge);
        config.addAllowedMethod(HttpMethod.DELETE);
        config.addAllowedMethod(HttpMethod.GET);
        config.addAllowedMethod(HttpMethod.OPTIONS);
        config.addAllowedMethod(HttpMethod.PUT);
        config.addAllowedMethod(HttpMethod.POST);
        ((UrlBasedCorsConfigurationSource) source).registerCorsConfiguration("/**", config);
       // config.setAllowCredentials(true);
       // config.addAllowedOrigin("*");
        //config.addAllowedHeader("*");
        //config.addAllowedMethod("*");
        //config.addAllowedOrigin(origin);

        return new CORSFilter(source);
    }
}
